package cz.tsystems.portablecheckin;

import cz.tsystems.base.BaseFragment;
import cz.tsystems.communications.CommunicationService;
import cz.tsystems.grids.PlanZakazkyActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class MainActivity extends BaseFragment {
	
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";
    private OnClickListener theButtonClickLisener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Button theButton = (Button)v;
			
			if(theButton.equals(getActivity().findViewById(R.id.btnSPZ)))
				getPlanZakzky();
		}
	};

    public MainActivity() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_main, container, false);
        
        return rootView;
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        setClickLiseners();
    	super.onActivityCreated(savedInstanceState);
    }
    
    @Override
    public void onStop() {
    	// TODO Auto-generated method stub
    	super.onStop();
    }
    
    @Override
    public void onPause() {
    	// TODO Auto-generated method stub
    	super.onPause();
    }
    
    private void setClickLiseners()
    {
    	Button btnSPZ = (Button)this.getActivity().findViewById(R.id.btnSPZ);
    	btnSPZ.setOnClickListener(theButtonClickLisener);
    }
    
    public void showData(Intent intent)
	{
		Log.d("SHOWDATA", intent.getExtras().toString());
		
		showPlanZakazky();
	}
	
	private void getPlanZakzky()
	{
		Intent msgIntent = new Intent(getActivity(), CommunicationService.class);
		
		msgIntent.putExtra("ACTIONURL", "pchi/CheckinOrderList");
    	msgIntent.putExtra("ACTION", "CheckinOrderList");		
    	getActivity().startService(msgIntent);
	}
    
    
    private void showPlanZakazky()
    {
    	
    	try {
			Intent i = new Intent(this.getActivity(), PlanZakazkyActivity.class);
			i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);// | Intent.FLAG_ACTIVITY_CLEAR_TOP);
			i.putExtra("type", PlanZakazkyActivity.eGrdTypes.eGRDPLANZAK);
			startActivityForResult(i, 2);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
}
